﻿using System;
using System.IO;

namespace KD.Sms.UI_Operate
{
    public class ImageSaver
    {
        private static string E_Root = "E:\\CyImage\\";
        private static string E_Directory = string.Empty;

        /// <summary>
        /// 保存采样图片
        /// </summary>
        /// <param name="samplingid"></param>
        /// <returns></returns>
        public static string DoSaveImage(int samplingid, System.Drawing.Image img, bool ifBefore)
        {
            string _path = string.Empty;
            try
            {
                E_Directory = E_Root + DateTime.Now.ToString("yyMMdd");
                if (!(System.IO.Directory.Exists(E_Directory)))
                    System.IO.Directory.CreateDirectory(E_Directory);
                if (img != null)
                {
                    if (ifBefore)
                        _path = E_Directory + "//" + samplingid.ToString() + "Before.jpg";
                    else
                        _path = E_Directory + "//" + samplingid.ToString() + "After.jpg";
                    img.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch (Exception ex)
            {
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "SaveImage.DoSaveImage()出错了");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
                _path = string.Empty;
            }
            return _path;
        }

        /// <summary>
        /// 保存采样图片
        /// </summary>
        /// <param name="samplingid"></param>
        /// <returns></returns>
        public static string DoSaveImage(string CarNum, System.Drawing.Image img)
        {
            string _path = string.Empty;
            try
            {
                E_Directory = E_Root + DateTime.Now.ToString("yyMMdd");
                if (!(System.IO.Directory.Exists(E_Directory)))
                    System.IO.Directory.CreateDirectory(E_Directory);
                if (img != null)
                {
                    _path = E_Directory + "//" + DateTime.Now.ToString("yyMMddHHmmss") + CarNum + ".jpg";
                    if (File.Exists(_path))
                        File.Delete(_path);
                    img.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch (Exception ex)
            {
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, "SaveImage.DoSaveImage()出错了");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Forms, ex.ToString());
                _path = string.Empty;
            }
            return _path;
        }
    }
}
